
const Async = require('async');
// app/service/user.js
const curYear = new Date().getFullYear();
const Service = require('egg').Service;
class UserService extends Service {
    
    async users( page = 0, limit = 10 ) {
        // 假如 我们拿到用户 id 从数据库获取用户详细信息
        let start = page * limit;
        console.time('users')
        let results = await this.app.mysql.select('xd_users', { // 搜索 post 表
            // where: {}, // WHERE 条件
            columns: ['user_id', 'nick_name', 'user_tag', 'stature', 'sex', 'birthday', 'city_code'], // 要查询的表字段
            // orders: [['user_id', 'desc']], // 排序方式
            limit: Number(limit), // 返回数据量
            offset: start, // 数据偏移量
        });
        console.timeEnd('users')
        console.time('img')
        let list = await relationTable(this, results, 'xd_user_images', {leftCode: 'user_id', rightCode: 'user_id'}, ['img_path']);
        console.timeEnd('img')
        console.time('cityList')
        let cityList = await relationTable(this, results, 'xd_region', {leftCode: 'city_code', rightCode: 'region_id'}, ['region_id', 'region_name']);
        console.timeEnd('cityList')
        results = results.map((item, index) => {
            item.img_path = list[index][0].img_path;
            item.city = cityList[index][0];
            if(!item.birthday) {
                item.age = 18;
            } else {
                item.age = curYear - Number(item.birthday.split('-')[0]);
            }
            return item;
        });
        
        return results;
    }

    async find (id) {
        const user = await this.app.mysql.get('xd_users', { user_id: id, });
        // 查询 image list
        const images = await this.app.mysql.select('xd_user_images', { 
            where: {user_id: 0},
            columns: ['img_path'], 
        });

        const region = await this.app.mysql.get('xd_region', { region_id: user.city_code, });
        const graduation = await this.app.mysql.get('xd_university', {university_id: user.graduation_school});
        
        const results = {
            ...user,
            img_list: images.map(item => item = item.img_path),
            region_name: region.region_name,
            university_name: graduation.university_name,
            university_lv: graduation.university_lv,
            
        };
        return results;
    }
    async info( page = 0, limit = 10 ) {
        let start = page * limit;
        console.log(limit);
        console.log(page);
        
        const results = await this.app.mysql.select('xd_users', { // 搜索 post 表
            // where: {}, // WHERE 条件
            columns: ['user_id', 'nick_name', 'user_tag', 'stature', 'sex', 'user_id'], // 要查询的表字段
            orders: [['user_id', 'desc']], // 排序方式
            limit: limit,   // 返回数据量
            offset: start,  // 数据偏移量
        });
        console.log(results);
        
        // const sql = `SELECT xu.nick_name, xu.user_tag, xu.stature, xu.sex, xu.user_id, xui.img_path
        //             FROM xd_users xu
        //             LEFT JOIN xd_user_images xui
        //             ON xu.user_id=xui.user_id
        //             ORDER BY user_id DESC limit ${start}, ${limit}`
        // const results = await this.app.mysql.query(sql, []);
        return results;
    }
    async test (results) {

    }
}

const relationTable = (_this, list, tableName, where, columns) => {
    return new Promise((resolve, reject) => {
        Async.mapLimit(list, 10, (item, callback) => {
            const obj = {};
            obj[where.rightCode] = item[where.leftCode];
            _this.app.mysql.select(tableName, { 
                where: obj,
                columns: columns,
             }).then(res => {
                callback(null, res); 
            })
        }, (err, results) => {
            if(err) {
                reject(err);
            }
            resolve(results);
        });
    });
}  



module.exports = UserService;



